package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //  1.拷贝属性
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 赋上用户 id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 2.查看当前用户的购物车是否有该商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (!CollectionUtils.isEmpty(shoppingCartList)) {    // 有该商品
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);   // 数量 + 1
            shoppingCartMapper.update(shoppingCart);
        } else {
            // 3.没有该商品,插入数据库

            // 判断该商品是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                // 添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                // 添加的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // 补全信息
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 插入数据库
            shoppingCartMapper.insertBatch(Collections.singletonList(shoppingCart));
        }
    }

    @Override
    public List<ShoppingCart> list() {
        // 1.从 ThreadLocal 中取出当前用户 id
        Long userId = BaseContext.getCurrentId();
        // 2.查询该用户的购物车
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.listByUserId(userId);
        return shoppingCartList;
    }

    @Override
    public void clean() {
        // 清空当前用户的购物车
        shoppingCartMapper.cleanByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void subGoods(ShoppingCartDTO shoppingCartDTO) {
        // 1. 拷贝属性
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 2.查看当前商品数量
        List<ShoppingCart> cartList = shoppingCartMapper.list(shoppingCart);
        shoppingCart = cartList.get(0);
        if(shoppingCart.getNumber() > 1) { // 数量大于 1，减 1
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.update(shoppingCart);
        }else{
            // 数量等于 1，删除数据
            setmealMapper.remove(shoppingCart);
        }
    }
}
